import { axios } from '@/utils/request'

const api = {
  user: 'manager/user/page',
  role: '/role',
  service: '/service',
  permission: '/permission',
  permissionNoPager: '/permission/no-pager',
  orgTree: '/org/tree'
}

export default api

export function getUserList(parameter) {
  return axios({
    url: api.user,
    method: 'get',
    params: parameter
  })
}

export function getRoleList(parameter) {
  return axios({
    url: api.role,
    method: 'get',
    params: parameter
  })
}

export function getServiceList(parameter) {
  return axios({
    url: api.service,
    method: 'get',
    params: parameter
  })
}

export function getPermissions(parameter) {
  return axios({
    url: api.permissionNoPager,
    method: 'get',
    params: parameter
  })
}

export function getOrgTree(parameter) {
  return axios({
    url: api.orgTree,
    method: 'get',
    params: parameter
  })
}

// id == 0 add     post
// id != 0 update  put
export function saveService(parameter) {
  return axios({
    url: api.service,
    method: parameter.id === 0 ? 'post' : 'put',
    data: parameter
  })
}

/**
 * 获取用户角色
 * @param userId
 */
export const getUserRoles = (userId) => {
  const params = {
    userId: userId
  }
  return axios({
    url: 'manager/user/roles',
    params,
    method: 'get'
  })
}
/**
 * 获取角色成员
 * @param roleId
 * @param userIds
 */
export const getRoleUsers = (roleId) => {
  const params = {
    roleId: roleId
  }
  return axios({
    url: 'role/users',
    params,
    method: 'get'
  })
}

/**
 * 获取所有角色
 */
export const getAllRoles = () => {
  return axios({
    url: 'role/all',
    method: 'get'
  })
}

/**
 * 获取所有用户列表
 */
export const getAllUsers = () => {
  return axios({
    url: 'manager/user/all',
    method: 'get'
  })
}

/**
 * 获取用户已分配权限
 * @param userId
 */
export const getAuthorityUser = (userId) => {
  const params = {
    userId: userId
  }
  return axios({
    url: 'authority/user',
    params,
    method: 'get'
  })
}

/**
 * 获取菜单和操作权限列表
 * @param serviceId
 */
export const getAuthorityMenu = () => {
  return axios({
    url: 'authority/menu',
    method: 'get'
  })
}
/**
 * 获取角色已分配权限
 * @param roleId
 */
export const getAuthorityRole = (roleId) => {
  const params = {
    roleId: roleId
  }
  return axios({
    url: 'authority/role',
    params,
    method: 'get'
  })
}
/**
 * 获取菜单和操作权限列表
 * @param serviceId
 */
export const getDictData = () => {
  return axios({
    url: 'dict/initDict',
    method: 'get'
  })
}

